<?php namespace Phphub\Vote;

use Evaluation;
use Issue;
use Lab;
use Comment;
use Opinion;
use Report;
use Reply;
use Auth;
use Topic;
use Vote;
use Carbon;
use User;

class Voter
{
    public $notifiedUsers = [];

    public function topicUpVote(Topic $topic)
    {
        if ($topic->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $topic->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $topic->decrement('vote_count', 1);
        } elseif ($topic->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $topic->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $topic->increment('vote_count', 2);
        } else {
            // first time click
            $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $topic->increment('vote_count', 1);

        }
    }

    public function topicDownVote(Topic $topic)
    {
        if ($topic->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // click second time for remove downvote
            $topic->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $topic->increment('vote_count', 1);
        } elseif ($topic->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // user already clicked upvote once
            $topic->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'downvote']);
            $topic->decrement('vote_count', 2);
        } else {
            // click first time
            $topic->votes()->create(['user_id' => Auth::id(), 'is' => 'downvote']);
            $topic->decrement('vote_count', 1);
        }
    }

    public function replyUpVote(Reply $reply)
    {
        if (Auth::id() == $reply->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }
        if ($reply->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $reply->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $reply->decrement('vote_count', 1);
        } elseif ($reply->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $reply->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $reply->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $reply->increment('vote_count', 2);
        } else {
            // first time click
            $reply->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $reply->increment('vote_count', 1);
        }

    }

    public function evaluationUpVote(Evaluation $evaluation)
    {
        if (Auth::id() == $evaluation->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }

        if ($evaluation->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $evaluation->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $evaluation->decrement('vote_count', 1);
        } elseif ($evaluation->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $evaluation->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $evaluation->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $evaluation->increment('vote_count', 2);
        } else {
            // first time click
            $evaluation->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $evaluation->increment('vote_count', 1);

        }
    }

    public function commentUpVote(Comment $comment)
    {
        if (Auth::id() == $comment->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }

        if ($comment->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $comment->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $comment->decrement('vote_count', 1);
        } elseif ($comment->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $comment->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $comment->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $comment->increment('vote_count', 2);
        } else {
            // first time click
            $comment->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $comment->increment('vote_count', 1);

        }
    }

    public function opinionUpVote(Opinion $opinion)
    {
        if (Auth::id() == $opinion->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }

        if ($opinion->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $opinion->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $opinion->decrement('vote_count', 1);
        } elseif ($opinion->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $opinion->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $opinion->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $opinion->increment('vote_count', 2);
        } else {
            // first time click
            $opinion->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $opinion->increment('vote_count', 1);

        }
    }

    public function issueUpVote(Issue $issue)
    {
        if (Auth::id() == $issue->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }

        if ($issue->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $issue->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $issue->decrement('vote_count', 1);
        } elseif ($issue->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $issue->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $issue->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $issue->increment('vote_count', 2);
        } else {
            // first time click
            $issue->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $issue->increment('vote_count', 1);

        }
    }

    public function issueDownVote(Issue $issue)
    {
        if ($issue->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // click second time for remove downvote
            $issue->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $issue->increment('vote_count', 1);
        } elseif ($issue->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // user already clicked upvote once
            $issue->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $issue->votes()->create(['user_id' => Auth::id(), 'is' => 'downvote']);
            $issue->decrement('vote_count', 2);
        } else {
            // click first time
            $issue->votes()->create(['user_id' => Auth::id(), 'is' => 'downvote']);
            $issue->decrement('vote_count', 1);
        }
    }

    public function reportUpVote(Report $report)
    {
        if (Auth::id() == $report->user_id) {
            return \Flash::warning(lang('Can not vote your feedback'));
        }

        if ($report->votes()->ByWhom(Auth::id())->WithType('upvote')->count()) {
            // click twice for remove upvote
            $report->votes()->ByWhom(Auth::id())->WithType('upvote')->delete();
            $report->decrement('vote_count', 1);
        } elseif ($report->votes()->ByWhom(Auth::id())->WithType('downvote')->count()) {
            // user already clicked downvote once
            $report->votes()->ByWhom(Auth::id())->WithType('downvote')->delete();
            $report->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $report->increment('vote_count', 2);
        } else {
            // first time click
            $report->votes()->create(['user_id' => Auth::id(), 'is' => 'upvote']);
            $report->increment('vote_count', 1);

        }
    }
}
